package com.oocl.servlet.action;

import static com.oocl.util.IPPropertiesUtil.getProperty;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.pojo.JSONResponse;
import com.oocl.pojo.Merchant;
import com.oocl.pojo.Protocol;
import com.oocl.service.MerchantManager;
import com.oocl.service.factory.MerchantManagerFactory;
import com.oocl.util.DESUtil;
import com.oocl.util.HTTPConnUtil;
import com.oocl.util.ValidateUtil;

@WebServlet("/sec/home/search.action")
public class SearchShopsActionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private MerchantManager merchantManager = MerchantManagerFactory.getInstance();
	ObjectMapper mapper = new ObjectMapper();
	String jsonStr;
	private final String GET_MERCHANTS_URL = String.format("http://%s:%s/O2OAdministrator/getMerchant", getProperty("admin.ipaddr"), getProperty("admin.port"));
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");                      //解决中文乱码
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("content-type","text/html;charset=UTF-8");//解决中文乱码
		
		Integer code=1;
		// receive parameter
		String searchType = request.getParameter("dtype");
		String searchKeyword = request.getParameter("keyword");
		
		Integer dType = 0;
		String keyword = null;
		if (!ValidateUtil.isNullOrEmpty(searchType)) {
			dType = Integer.parseInt(searchType);
		}
		if (!ValidateUtil.isNullOrEmpty(searchKeyword)) {
			keyword = searchKeyword;
		}
		System.out.println(keyword);
		// get allowed mid list
		String merchantsJson = HTTPConnUtil.sendGet(GET_MERCHANTS_URL);
		System.out.println(merchantsJson);
		if (ValidateUtil.isNullOrEmpty(merchantsJson)) {
			code=-1;
			JSONResponse<Merchant> jsonResponse = new JSONResponse<Merchant>();
			jsonResponse.setCode(code);
			jsonResponse.setMsg("平台端异常!");
			jsonStr = mapper.writeValueAsString(jsonResponse);
			PrintWriter out = response.getWriter();
			out.println(jsonStr);
			return;
		}
		merchantsJson = merchantsJson.replaceAll(" ", "+");
		merchantsJson = new DESUtil().decrypt(merchantsJson,"UTF-8");
		System.out.println(merchantsJson);
		Protocol p = new ObjectMapper().readValue(merchantsJson, Protocol.class);	
		@SuppressWarnings("unchecked")
		List<String> listObj = (List<String>) p.getMsgObj();
		List<String> allAllowedMid = merchantManager.MerchantsIdFilterByStatus(listObj);
		
		
//		List<String> allAllowedMid = new ArrayList<String>();//test
//		allAllowedMid.add("61e31569-e8c1-4fbe-a737-7e499ade1c94");//test
		

		//转化为Json
		List<Merchant> merchantsList = merchantManager.findMerchantsByMIdList(keyword, dType, allAllowedMid);
		JSONResponse<Merchant> jsonResponse = new JSONResponse<Merchant>(code,null,null,merchantsList);
		jsonStr = mapper.writeValueAsString(jsonResponse);
		PrintWriter out = response.getWriter();
		out.println(jsonStr);
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		doPost(request, response);
	}
}
